#include "constants.h"
#include "types.h"
#include "defs.h"
#include "mm.h"
#include "x86.h"
#include "proc.h"

// Interrupt descriptor table (shared by all CPUs).
struct gatedesc idt[256];
extern uint vectors[];

void tvinit(void) {
	int i = 0;
	for (i = 0; i < 256; i++)
		SETGATE(idt[i], 0, SEG_KCODE<<3, vectors[i], 0);
	SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER);
}

void idtinit(void) {
	lidt(idt, sizeof(idt));
}

void trap(struct trapframe *tf) {

	if (tf->trapno == T_SYSCALL) {
		proc->tf = tf;
		syscall();
		return;
	}

	switch(tf->trapno){
		case T_IRQ0 + IRQ_KBD:
				kbdintr();
				break;
	}


//	if(proc)
//		printk("%d\n",proc->pid);
//	printk("interrupt no.=%d , cpuno= %d\n", tf->trapno, cpu->id);
//	printk("abc\n");
	lapiceoi();

	if(proc && proc->state == RUNNING && tf->trapno == T_IRQ0+IRQ_TIMER){
	    yield();
	}
}
